# coding:utf-8
from selenium import webdriver
import time


# 云后台登录相关测试

class CloudAdminLoginTest:
    driver = None

    def __init__(self):
        self.driver = webdriver.Chrome();

    def __init__(self, driver):
        self.driver = driver;

    # 错误登录
    def errorLogin(self):
        arrays = [{"key": "错误登录", "account": "123123123123", "password": "112234", "xpath": "/html/body/div[1]/form/p",
                   "checkValue": "用户不存在"},
                  {"key": "账户/密码最长限制登录", "account": "123456321578451236541", "password": "121212311231231312334",
                   "xpath": "/html/body/div[1]/form/p", "checkValue": "请输入正确的账号或密码（6-20位）"},
                  {"key": "账户/密码最短限制登录", "account": "1", "password": "1234",
                   "xpath": "/html/body/div[1]/form/p", "checkValue": "请输入正确的账号或密码（6-20位）"},
                  {"key": "中文登录", "account": "测试TESTstem123", "password": " 测试登录123dfasd",
                   "xpath": "/html/body/div[1]/form/p", "checkValue": "请输入正确的账号或密码（6-20位）"},
                  {"key": "乱码登录", "account": "@%@&!%*&^*$#@", "password": "@%@&!%*&^*$#@",
                   "xpath": "/html/body/div[1]/form/p","checkValue": "用户不存在"},
                  {"key": "字母登录", "account": "wkasaaakasdasd", "password": "asdadawwesdaad",
                   "xpath": "/html/body/div[1]/form/p", "checkValue": "用户不存在"},
                  {"key": "无账号密码登录", "account": None, "password": None,
                   "xpath": "/html/body/div[1]/form/p","checkValue": "请输入账号"},
                  {"key": "混合限制登录1", "account": "1234dkjasddasda236541", "password": "1234",
                   "xpath": "/html/body/div[1]/form/p", "checkValue": "请输入正确的账号或密码（6-20位）"},
                  {"key": "混合限制登录2", "account": "1234", "password": "123456321578451236541",
                   "xpath": "/html/body/div[1]/form/p", "checkValue": "请输入正确的账号或密码（6-20位）"},
                  {"key": "混合限制登录3", "account": "12%g程D%&", "password": "程213)(&%)",
                   "xpath": "/html/body/div[1]/form/p", "checkValue": "请输入正确的账号或密码（6-20位）"},
                  {"key": "混合限制登录4", "account": "12%g程D%&hogdjasdd乘法斯科拉极大可视对讲安康$$^&(hf4",
                   "password": "程213)(&%)GFGHGG阿达大就是坑爹花生酱的华手机号都啊可接受的和阿斯顿金卡解释道好就是HJVHJ程",
                   "xpath": "/html/body/div[1]/form/p", "checkValue": "请输入正确的账号或密码（6-20位）"},
                  ]
        resArrays = [{}, {}, {},{},{},{},{},{},{},{},{}]
        for i in range(len(arrays)):
            res = self.loginImpl(arrays[i]['account'], arrays[i]['password'], arrays[i]['xpath'],
                                 arrays[i]['checkValue'])
            resArrays[i]['key'] = arrays[i]['key']
            resArrays[i]['res'] = res

        return resArrays

    # 正确登录
    def login(self):
        res = self.loginImpl("chengruisong", "chengruisong", "/html/body/header/span", '退出')

    # 登录基础方法
    def loginImpl(self, account, password, checkXpath, checkValue):
        time.sleep(1)
        # 打开地址
        self.driver.get("http://192.168.1.164:8088/mycloud/login.html")
        # 正确的用户名
        if (account != None or password != None):
            self.driver.find_element_by_xpath("/html/body/div[1]/form/input[1]").send_keys(account)
            self.driver.find_element_by_xpath('/html/body/div[1]/form/input[2]').send_keys(password)
        self.driver.find_element_by_class_name("sub").click()

        time.sleep(1)
        # 判断错误提示的元素是否存在
        if self.driver.find_element_by_xpath(checkXpath).text == checkValue:
            return True
        else:
            return False
